package Controller;

import Data.FriendListData;
import Data.PrivateInformationData;
import Data.ResetPwdData;
import Data.event;
import MyInterfaces.MassageType;
import Service.ClientConnectionServer;
import Service.GetMassage;
import Service.Main;
import Service.SendMassage;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ResetPwd {

    @FXML
    private PasswordField ensurePassword;

    @FXML
    private TextField Emailnumber;

    @FXML
    private Button sendCaptcha;

    @FXML
    private PasswordField getNewPassword;

    @FXML
    private Button ensureButton;

    @FXML
    private TextField textpassword1;

    @FXML
    private Button closeButton;

    @FXML
    private TextField textpassword2;

    @FXML
    private TextField getCaptcha;

    @FXML
    private Button cansee1;

    @FXML
    private Button cansee2;

    @FXML
    private Button notsee2;

    @FXML
    private Button notsee1;
    String yzm=null;
    private static Date predate=new Date();

    public ResetPwd() throws IOException {
    }
    private static Socket socket;
    public static void init(Socket socket) throws IOException {
        ResetPwd.socket=socket;
        predate.setTime(predate.getTime()-60*1000);
    }

    @FXML
    void ensureAction(ActionEvent event) {

    }

    @FXML
    void closeAction(ActionEvent event) {

    }

    @FXML
    void sendCaptchaNumber(ActionEvent event) {

    }

    String email="";
    public void ensureAction(javafx.event.ActionEvent actionEvent) throws Exception {
        String Pwd;
        //判断当前是哪种状态
        if(textpassword1.isVisible())
        {
            Pwd=textpassword1.getText();
        }
        else Pwd=getNewPassword.getText();

        email=Emailnumber.getText();
        if(getNewPassword.getText().equals("")||ensurePassword.getText().equals("")||
        getCaptcha.getText().equals("")||email.equals(""))
        {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("您还没有填写完!");
            alert.showAndWait();
        }
        else if(!getNewPassword.getText().equals(ensurePassword.getText()))
        {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("密码俩次不一样，请重写!");
            alert.showAndWait();
        }
        else {
            String pwdPattern ="^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^\\da-zA-Z\\s]).{8,16}$";
            Pattern p=Pattern.compile(pwdPattern);
            Matcher m=p.matcher(getNewPassword.getText());
            if(m.find())
            {
                //通过了正则表达式
                System.out.println("可以");
            }
            else
            {
                System.out.println(getNewPassword.getText());
                System.out.println(pwdPattern);
                //没有通过
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("错误");
                alert.setHeaderText(null);
                alert.setContentText("密码强度不够，必须是8-16位，需要有字母，数字，和特殊字符^_^");
                alert.showAndWait();
                return ;
            }

            ResetPwdData resetPwdData=new ResetPwdData(email,getCaptcha.getText(),getNewPassword.getText());
            event massage=new event();
            massage.setType(MassageType.Request_Reset_Password);
            massage.setT(resetPwdData);
            new SendMassage().sendMassage(massage);

            event returnmassage=(event) new GetMassage().getMassage();
            if(returnmassage.getType().equals(MassageType.Return_Reset_Password_Success))
            {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("成功");
                alert.setHeaderText(null);
                alert.setContentText("重置密码成功!");
                alert.showAndWait();

                //直接进入主界面
                Stage thisstage=(Stage) ensureButton.getScene().getWindow();
                thisstage.close();

                //直接进入主界面
                FriendListData friendListData=(FriendListData) returnmassage.getT();
                ArrayList<PrivateInformationData> friendlist=friendListData.getFriendList();
                PrivateInformationData mydata=friendListData.getMydata();

                Stage stage=new Stage();
                File file=new File("E:\\JDK\\QQClient\\nox\\src\\fxml\\ChatMain.fxml");
                URL location =file.toURI().toURL();
                FXMLLoader fxmlLoader=new FXMLLoader();
                fxmlLoader.setLocation(location);
                Parent root=fxmlLoader.load();
                stage.setTitle("主界面");
                stage.setResizable(false);
                stage.setScene(new Scene(root,400,700));
                stage.show();

                Main.AllContronller.setChatMainController(fxmlLoader.getController());
                Main.AllContronller.getChatMainController().setSocket(socket);
                Main.AllContronller.getChatMainController().setChatStage(mydata,friendlist);

                Thread myThread=new Thread(new ClientConnectionServer(socket,mydata.getUserid()));
                myThread.start();

            }
            else if(returnmassage.getType().equals(MassageType.Return_Reset_Password_Fail))
            {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("错误");
                alert.setHeaderText(null);
                alert.setContentText("有可能是你的邮箱和验证码输入错误，或者你的验证码已经过期!");
                alert.showAndWait();
            }
        }
    }

    public void closeAction(javafx.event.ActionEvent event) {
        Stage stage = (Stage) closeButton.getScene().getWindow();
        stage.close();
    }

    public void sendCaptchaNumber(javafx.event.ActionEvent actionEvent) throws Exception {
        email=Emailnumber.getText();
        if(email.equals(""))
        {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("你还没有输入邮箱地址");
            alert.showAndWait();
            return ;
        }
        String emailPattern="^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$";
        Pattern p=Pattern.compile(emailPattern);
        Matcher m=p.matcher(email);
        if(m.find())
        {
            System.out.println("邮箱可以的");

            Date nowdate=new Date();
            if((nowdate.getTime()-predate.getTime())< 60 * 1000)
            {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("错误");
                alert.setHeaderText(null);
                alert.setContentText("一分钟内不可以重复点击获取验证码!");
                alert.showAndWait();
                return ;
            }

            event massage=new event();
            massage.setType(MassageType.Get_Captcha);
            massage.setT(new String(email));

            new SendMassage().sendMassage(massage);
            //发送邮箱
            predate=new Date();
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("成功");
            alert.setHeaderText(null);
            alert.setContentText("发送成功");
            alert.showAndWait();
        }
    }

    public void CanSeeAction1(javafx.event.ActionEvent actionEvent) {
        cansee1.setVisible(false);
        textpassword1.setVisible(false);
        notsee1.setVisible(true);
        getNewPassword.setText(textpassword1.getText());
        getNewPassword.setVisible(true);
    }


    public void NotSeeAction1(javafx.event.ActionEvent actionEvent) {
        cansee1.setVisible(true);
        textpassword1.setText(getNewPassword.getText());
        textpassword1.setVisible(true);
        notsee1.setVisible(false);
        getNewPassword.setVisible(false);
    }

    public void CanSeeAction2(javafx.event.ActionEvent actionEvent) {
        cansee2.setVisible(false);
        textpassword2.setVisible(false);
        notsee2.setVisible(true);
        ensurePassword.setText(textpassword2.getText());
        ensurePassword.setVisible(true);
    }
    public void NotSeeAction2(javafx.event.ActionEvent actionEvent) {
        cansee2.setVisible(true);
        textpassword2.setText(ensurePassword.getText());
        textpassword2.setVisible(true);
        notsee2.setVisible(false);
        ensurePassword.setVisible(false);
    }

}
